import {
  getKpiCard, // 指标卡数据
  getTopRank, // 活跃排行
  getBottomRank, // 休眠排行
  getActiveTrend, // 活跃走势
  getMonitorTenantById, // 根据ID查询活跃租户
  getMonitorTenantPage, // 分页查询活跃租户
  getPageOperationRecord, // 分页查询操作记录
  getStayTimeTrend, // 停留时间趋势图
} from '@/api/monitor'

const getMonitorSata = () => {
  return {
    kpiCardData: {}, // 指标卡数据
    topRankData: [], // 活跃排行
    bottomRankData: [], // 休眠排行
    activeTrendData: {}, // 活跃走势
    monitorTenantByIdData: {}, // 根据ID查询活跃租户
    tenantPageData: {}, // 分页查询活跃租户
    operationRecordData: {}, // 分页查询操作记录
    stayTimekData: {}, // 停留时间趋势图
  }
}
  
const state = getMonitorSata()

const mutations = {
  RESET_STATE: state => {
    Object.assign(state, getMonitorSata())
  },
}

const actions = {
  // 获取销售指标卡数据
  async getKpiCard({state}, payload = {}) {
    state.loading = true
    const {retData = {}} = await getKpiCard(payload)
    Object.assign(state, {
      loading: false,
      kpiCardData: retData || {}
    })
  },
  // 活跃排行
  async getTopRank({state}, payload = {}) {
    state.loading = true
    const {retData = {}} = await getTopRank(payload)
    Object.assign(state, {
      loading: false,
      topRankData: retData || {}
    })
  },
  // 休眠排行
  async getBottomRank({state}, payload = {}) {
    state.loading = true
    const {retData = {}} = await getBottomRank(payload)
    Object.assign(state, {
      loading: false,
      bottomRankData: retData || {}
    })
  },
  // 活跃走势
  async getActiveTrend({state}, payload = {}) {
    state.loading = true
    const {retData = []} = await getActiveTrend(payload)
    const rows = retData || []
    const columns = ['xxAlias', 'yyValue']
    Object.assign(state, {
      loading: false,
      activeTrendData: {
        rows,
        columns
      }
    })
  },
  // 根据ID查询活跃租户
  async getMonitorTenantById({state}, payload = {}) {
    state.loading = true
    const {retData = {}} = await getMonitorTenantById(payload)
    Object.assign(state, {
      loading: false,
      monitorTenantByIdData: retData || {}
    })
  },
  // 分页查询活跃租户
  async getMonitorTenantPage({state}, payload = {}) {
    state.loading = true
    const {retData = {}} = await getMonitorTenantPage(payload)
    Object.assign(state, {
      loading: false,
      tenantPageData: retData || {}
    })
  },
  // 分页查询操作记录
  async getPageOperationRecord({state}, payload = {}) {
    state.loading = true
    const {retData = {}} = await getPageOperationRecord(payload)
    Object.assign(state, {
      loading: false,
      operationRecordData: retData || {}
    })
  },
  // 停留时间趋势图
  async getStayTimeTrend({state}, payload = {}) {
    state.loading = true
    const {retData = []} = await getStayTimeTrend(payload)
    const rows = retData || []
    const columns = ['xxAlias', 'yyValue']
    Object.assign(state, {
      loading: false,
      stayTimekData: {
        rows,
        columns
      }
    })
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
